//
//  frog.swift
//  FrogGame
//
//  Created by wanght on 2016/12/1.
//  Copyright © 2016年 wanght. All rights reserved.
//

import SpriteKit

enum Status : Int {
    case stand = 1, jump
}

class Frog : SKSpriteNode {
    
    var status = Status.stand
    var oneTimeInterval = 0.15
    
    init()
    {
        let texture = SKTexture(imageNamed: "normal_1.png")
        let size = CGSize(width: 100, height: 120)
        super.init(texture: texture, color: UIColor.white, size: size)
        
        
        standFrog()

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    func standFrog() {
        
        let animationTextures = (1...15).map {
            SKTexture(imageNamed: "normal_\($0)")
        }
        
        let popAction = SKAction.animate(with: animationTextures, timePerFrame: 1 / 8.0)
        let animateSequence = SKAction.sequence([popAction, SKAction.wait(forDuration: 3.0)])
        
        let repeateAction = SKAction.repeatForever(animateSequence)
        
        self.removeAllActions()
        self.run(repeateAction)
        self.status = .stand
    }
    
    
    func jumpFrog() {
        
        self.status = .jump

        let animationTextures = (1...11).map {
            SKTexture(imageNamed: "jump_\($0)")
        }
        
        let animateAction = SKAction.animate(with: animationTextures, timePerFrame: 1 / 22)
        
        let moveUp = SKAction.moveBy(x: 0, y: 100, duration: oneTimeInterval)
        let moveSequence = SKAction.sequence([moveUp, moveUp.reversed()])
        
        let groupAction = SKAction.group([animateAction, moveSequence])
        
        self.removeAllActions()
        self.run(groupAction, completion:{self.status = .stand;self.standFrog()})
    }
    
    func jumpWaterFrog() {
        
        let frogTextures = (1...12).map {
            SKTexture(imageNamed: "jump_water_\($0)")
        }
        let frogAction = SKAction.animate(with: frogTextures, timePerFrame: 1 / 60)
        let waterTextures = (13...32).map {
            SKTexture(imageNamed: "jump_water_\($0)")
        }
        let waterAction = SKAction.animate(with: waterTextures, timePerFrame: 1 / 100)
        let animateAction = SKAction.sequence([frogAction, waterAction, frogAction.reversed()])
        
        oneTimeInterval = 0.1
        
        let moveUpLow = SKAction.moveBy(x: 0, y: 100, duration: oneTimeInterval)
        let moveUpHigh = SKAction.moveBy(x: 0, y: 150, duration: oneTimeInterval)
        let moveSequence = SKAction.sequence([moveUpLow, moveUpHigh.reversed(), SKAction.wait(forDuration: 0.2), moveUpHigh, moveUpLow.reversed()])
        
        let groupAction = SKAction.group([animateAction, moveSequence])
        
        self.removeAllActions()
        self.run(groupAction, completion: {self.status = .stand;self.standFrog()})
        self.status = .jump
    }
}
